<template>
  <div class="writing-practice">
    <div class="container">
      <h2>描字贴</h2>
      
      <div class="content-box">
        <div class="input-section">
          <el-input
            type="textarea"
            v-model="inputText"
            :rows="5"
            placeholder="我是xiyueta小助手，请问有什么可以帮您?"
          />
        </div>

        <div class="style-section">
          <div class="style-item">
            <span class="label">格子类型：</span>
            <el-radio-group v-model="gridType">
              <el-radio-button value="田字格">田字格</el-radio-button>
              <el-radio-button value="米字格">米字格</el-radio-button>
              <el-radio-button value="回宫格">回宫格</el-radio-button>
            </el-radio-group>
          </div>

          <div class="style-item">
            <span class="label">格子颜色：</span>
            <el-radio-group v-model="gridColor">
              <el-radio-button value="green">绿色</el-radio-button>
              <el-radio-button value="black">黑色</el-radio-button>
              <el-radio-button value="red">红色</el-radio-button>
            </el-radio-group>
          </div>
        </div>

        <div class="action-buttons">
          <el-button type="primary" @click="generatePractice">
            <el-icon><DocumentAdd /></el-icon>
            生成字帖
          </el-button>
          <el-button type="success" @click="printPractice">
            <el-icon><Printer /></el-icon>
            打印字帖
          </el-button>
          <el-button @click="testPreview">
            <el-icon><DocumentChecked /></el-icon>
            测试用例
          </el-button>
          <el-button @click="clearContent">
            <el-icon><Delete /></el-icon>
            清空
          </el-button>
          <el-button @click="goHome">
            <el-icon><Back /></el-icon>
            返回首页
          </el-button>
        </div>

        <div class="preview-section" ref="previewSection">
          <div v-for="(char, index) in characters" 
               :key="index"
               class="grid-item"
               :class="gridType">
            <div class="grid-lines" :style="{borderColor: gridColor}"></div>
            <span class="character">{{ char }}</span>
          </div>
        </div>

        <div class="help-text">
          <h3>帮助说明：</h3>
          <p>
            1. 在输入框中输入需要练习的文字<br>
            2. 选择格子类型（田字格/米字格/回宫格）和格子颜色<br>
            3. 点击"生成字帖"按钮生成练习模板<br>
            4. 可以使用"打印字帖"功能打印出来练习<br>
            5. "测试用例"可以快速查看效果，"清空"可以重新输入
          </p>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import { useRouter } from 'vue-router'
import { 
  DocumentAdd, 
  Printer, 
  DocumentChecked,
  Delete,
  Back
} from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'

const router = useRouter()
const inputText = ref('')
const gridType = ref('田字格')
const gridColor = ref('green')
const previewSection = ref(null)

const characters = computed(() => {
  return inputText.value.split('')
})

const generatePractice = () => {
  if (!inputText.value.trim()) {
    ElMessage.warning('请输入需要练习的文字')
    return
  }
  // 生成字帖逻辑
}

const printPractice = () => {
  if (!characters.value.length) {
    ElMessage.warning('请先生成字帖再打印')
    return
  }
  window.print()
}

const testPreview = () => {
  inputText.value = '我是xiyueta小助手，请问有什么可以帮您?'
}

const clearContent = () => {
  inputText.value = ''
}

const goHome = () => {
  router.push('/')
}
</script>

<style lang="scss" scoped>
.writing-practice {
  padding: 20px;
  max-width: 1200px;
  margin: 0 auto;

  h2 {
    text-align: center;
    color: #2c3e50;
    margin-bottom: 30px;
    font-size: 32px;
    font-weight: 600;
    text-shadow: 2px 2px 4px rgba(0,0,0,0.1);
  }

  .content-box {
    background: #fff;
    border-radius: 16px;
    box-shadow: 0 10px 30px rgba(0,0,0,0.1);
    padding: 30px;

    .input-section {
      margin-bottom: 30px;

      :deep(.el-textarea__inner) {
        font-size: 16px;
        padding: 15px;
        border-radius: 8px;
        resize: none;
      }
    }

    .style-section {
      margin-bottom: 30px;

      .style-item {
        display: flex;
        align-items: center;
        margin-bottom: 20px;

        &:last-child {
          margin-bottom: 0;
        }

        .label {
          width: 100px;
          color: #606266;
          font-size: 16px;
        }
      }
    }

    .action-buttons {
      display: flex;
      gap: 15px;
      margin-bottom: 30px;
      justify-content: center;
    }

    .preview-section {
      display: grid;
      grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
      gap: 20px;
      padding: 20px;
      background: #f8fafc;
      border-radius: 8px;
      margin-bottom: 30px;
      min-height: 200px;

      .grid-item {
        position: relative;
        width: 100px;
        height: 100px;
        margin: 0 auto;

        &.田字格 {
          .grid-lines {
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            border: 2px solid;
            
            &::before,
            &::after {
              content: '';
              position: absolute;
              background-color: currentColor;
            }

            &::before {
              left: 50%;
              top: 0;
              bottom: 0;
              width: 2px;
              transform: translateX(-50%);
            }

            &::after {
              top: 50%;
              left: 0;
              right: 0;
              height: 2px;
              transform: translateY(-50%);
            }
          }
        }

        &.米字格 {
          .grid-lines {
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            border: 2px solid;

            &::before,
            &::after {
              content: '';
              position: absolute;
              width: 141%;
              height: 2px;
              background-color: currentColor;
              top: 50%;
              left: 50%;
            }

            &::before {
              transform: translate(-50%, -50%) rotate(45deg);
            }

            &::after {
              transform: translate(-50%, -50%) rotate(-45deg);
            }
          }
        }

        &.回宫格 {
          .grid-lines {
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            border: 2px solid;
          }
        }

        .character {
          position: absolute;
          top: 50%;
          left: 50%;
          transform: translate(-50%, -50%);
          font-size: 40px;
          color: #666;
          z-index: 1;
        }
      }
    }

    .help-text {
      background: #f8fafc;
      padding: 20px;
      border-radius: 8px;
      margin-bottom: 30px;
      text-align: left;

      h3 {
        color: #2c3e50;
        margin: 0 0 10px 0;
        font-size: 18px;
        font-weight: 600;
      }

      p {
        color: #666;
        margin: 0;
        line-height: 1.6;
        font-size: 14px;
      }
    }
  }
}

@media screen and (max-width: 768px) {
  .writing-practice {
    padding: 10px;

    h2 {
      font-size: 24px;
      margin-bottom: 20px;
    }

    .content-box {
      padding: 15px;

      .style-section {
        .style-item {
          flex-direction: column;
          align-items: flex-start;
          gap: 10px;

          .label {
            width: auto;
          }
        }
      }

      .action-buttons {
        display: grid;
        grid-template-columns: repeat(2, 1fr);
        gap: 8px;
        padding: 0 10px;

        .el-button {
          margin: 0;
          width: 100%;
          height: 40px;
          padding: 8px 15px;
          font-size: 14px;
          display: flex;
          align-items: center;
          justify-content: center;
          gap: 5px;

          .el-icon {
            margin-right: 4px;
          }
        }

        .el-button:last-child {
          grid-column: 1 / -1;
          background-color: #909399;
          color: white;
        }
      }

      .preview-section {
        grid-template-columns: repeat(auto-fill, minmax(80px, 1fr));
        gap: 10px;
        padding: 10px;

        .grid-item {
          width: 80px;
          height: 80px;

          .character {
            font-size: 32px;
          }
        }
      }
    }
  }
}

@media print {
  .writing-practice {
    padding: 0;

    h2,
    .input-section,
    .style-section,
    .action-buttons,
    .help-text {
      display: none;
    }

    .content-box {
      box-shadow: none;
      padding: 0;
    }

    .preview-section {
      background: none;
    }
  }
}
</style> 